/**
 * 用户角色持久模块
 */
define('store/UserRoleStore', ['WxCore','restTemplate'], function (wxCore,restTemplate) {

    const module= {
        storeName: 'UserRoleStore',
        namespaced: true,
        state: {},
        mutations: {},
        actions: {
            getValidateRule() {
                return {
                    roleName: [
                        { required: true, message: '角色名称不能为空', trigger: 'blur' }
                    ]
                }
            },list({state, commit}, {pageIndex, pageSize}) {
                return restTemplate.request({
                    url: '/api/OauthRole/list',
                    params: {
                        pageIndex,
                        pageSize
                    },
                    method: 'get'
                })
            },
            listRoleResourcs ({ state, commit }, { clientId, roleId }) {
                return restTemplate.request({
                    url: '/api/OauthRole/resourcs',
                    params: { clientId, roleId },
                    method: 'get'
                })
            },
            save({state, commit}, vo) {
                const requestUrl = (vo.roleId && vo.roleId !== '') ? '/api/OauthRole/update' : '/api/OauthRole/add';
                return restTemplate.request({
                    url: requestUrl,
                    method: 'post',
                    data: vo
                })
            },
            saveResources ({ state, commit }, vo) {
                const requestUrl = '/api/OauthRole/saveResource'
                return restTemplate.request({
                    url: requestUrl,
                    method: 'post',
                    data: vo
                })
            },
            delete({state, commit}, roleId) {
                return restTemplate.request({
                    url: 'api/OauthRole/delete',
                    method: 'post',
                    data: {id: roleId},
                    transformRequest: [function (data) {
                        // Do whatever you want to transform the data
                        let temData=JSON.parse(data);
                        let ret = ''
                        for (let it in temData) {
                            ret += encodeURIComponent(it) + '=' + encodeURIComponent(temData[it]) + '&'
                        }
                        return ret
                    }],
                    headers: {
                        'Content-Type': 'application/x-www-form-urlencoded'
                    }
                })
            },
            listRoleAll({state, commit}) {
                return restTemplate.request({
                    url: '/api/OauthRole/all'
                })
            }
        }
    };
    return module;
})